
var app = getApp();
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onAuth() {
    wx.getSetting({
      success: function(res){
          if (res.authSetting['scope.userInfo']){
          wx.login({
            //获取openid
            success:function(res){
              var code =res.code;
              wx.getUserInfo({
                success: function (ros) {
                  var userNick = ros.userInfo.nickName;//用户昵称
                  var avataUrl = ros.userInfo.avatarUrl;//用户头像地址
                  var gender = ros.userInfo.gender;//用户性别
                  wx.request({
                    url: 'https://wfbg.etshfw.cn/Home/User/saveOpenId',
                    data: {
                      code: code,
                      nick: userNick,
                      avaurl: avataUrl,
                      sex: gender,
                    },
                    header: { 'content-type': 'application/json' },
                    method: "POST",
                    success: function (rqs) {
                        if (rqs.data.status == "yes")
                        {   
                            wx.setStorage({
                                key: "openid",
                                data: rqs.data.openid
                            })
                        } 
                        else if (rqs.data.status == "no1")
                        {
                            console.log(rqs.data.str)
                            wx.navigateTo({
                                url: '../index/index',
                            })
                        } 
                        else if (rqs.data.status == "no2") 
                        {
                            console.log(rqs.data.str);
                            wx.navigateTo({
                                url: '../index/index',
                            })
                        } 
                        
                    }
                  })
                  app.globalData.userInfo = ros.userInfo;
                }
              })
            }
          })
        wx.navigateTo({
            url: '../index/index',
          })
        }else{
              wx.navigateTo({
            url: '../auth/auth',
        })
        }
      }
    })
  }
})